﻿using IQIGame.Onigao.GamePlay;
using LFloatMath.Collision2D;
using LFloatMath.Math;

namespace IQIGame.Onigao.Logic
{
    public class CollisionComponent : TComponent
    {
        public Unit unit;
        public ColliderProxy proxy;

        private bool mInitialized;
        
        public void InitializeCircle(LFloat fRadius)
        {
            if (this.mInitialized)
            {
                return;
            }
            
            proxy = ColliderProxy.Fetch();
            proxy.Init(CollisionUtil.GetCirclePrefab(fRadius), unit?.pos ?? LVector2.zero);
            proxy.IsStatic = false;
            proxy.LayerType = 0;
            
            this.unit?.battle.playCtrl.collisionCtrl.AddCollider(this);
            this.mInitialized = true;
        }

        public void InitializeCub(LFloat fWidth, LFloat fHeight, LVector2 fAngle)
        {
            if (this.mInitialized)
            {
                return;
            }

            proxy = ColliderProxy.Fetch();
            proxy.Init(CollisionUtil.GetCubePrefab(fWidth, fHeight, fAngle), unit?.pos ?? LVector2.zero);
            proxy.IsStatic = false;
            proxy.LayerType = 0;

            this.unit?.battle.playCtrl.collisionCtrl.AddCollider(this);
            this.mInitialized = true;
        }
        
        protected override void OnDispose()
        {
            this.unit?.battle.playCtrl.collisionCtrl.RmoveCollider(this);
            ColliderProxy.Free(this.proxy);
            this.mInitialized = false;
        }

        public override void OnCreate(TEntity entity)
        {
            this.mInitialized = false;
            this.unit = entity as Unit;
        }
    }
}